home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: bristol.com!handel!dan
- From: dan@bristol.com (J. Daniel Smith)
- Subject: Re: Q: realloc->free?
- Sender: usenet@bristol.com (USENET News System)
- Organization: Bristol Technology Inc.
- References: <4df2ud$706@oxy.rust.net> <4dgic7$qin@unix.sri.com> <dan.821890778@handel> <821904243snz@genesis.demon.co.uk>
- Message-ID: <dan.821977599@handel>
- Date: Thu, 18 Jan 1996 15:06:39 GMT
-
- In <821904243snz@genesis.demon.co.uk> Lawrence Kirby <fred@genesis.demon.co.uk> writes:
- >[...]
- >>adding code to deal for the extremely rare case can add
- >>significantly to the complexity of the code.
-
- >Adding error checking for heap allocation makes minimal difference to
- >program complexity. OTOH dealing with rare cases properly makes the task
- >of debugging much easier.
-
- Here's an example of what I had in mind
- void f(const char *string)
- {
- static char *buf = NULL;
-
- buf = realloc(buf, strlen(string) + 1);
- /* ... do whatever with buf ... */
- }
- Dealing with the realloc() failure makes this
- void f(const char *string)
- {
- static char *buf = NULL;
- char *tmp;
-
- if ((tmp = realloc(buf, strlen(string) + 1)) == NULL)
- return; /* realloc() failed...now what? */
- buf = tmp;
- /* ... do whatever with buf ... */
- }
- This prevents a crash, and in that respect it is definately more
- robust than not checking realloc()'s return value. But does the
- program still run correctly? Not likely, since whatever f() was
- supposed to do isn't being done.
-
- One could make all kinds of arguments about return values, error codes,
- poor software design, whatever. And I'll agree that things would be
- better if f() were written as follows
- int f(const char *string)
- {
- static char *buf = NULL;
- char *tmp;
-
- if ((tmp = realloc(buf, strlen(string) + 1)) == NULL)
- return 0; /* realloc() failed */
- buf = tmp;
- /* ... do whatever with buf ... */
-
- return 1;
- }
- but in real life, this sometimes isn't an option.
-
- >> And if realloc() does
- >>fail, there are probably a lot bigger things to worry about than
- >>leaking memory (like the GUI not being able to create a window to tell
- >>you something has went wrong).
- >
- >You can at the very least exit cleanly at the point of failure. If you fail
- >in allocation you can try to creewate an error window and exit cleanly
- >if that fails.
-
- yes, this is nicer than crashing and leaving a core dump, or getting a
- GPF dialog box. But a GUI application suddenly quitting for no
- apparent reason isn't very nice either.
-
- >>Yes, the programmer needs to be aware of the fact that realloc() can
- >>fail, but in some cases ALWAYS checking the return value of realloc()
- >>just isn't practical given other constraints.
- >
- >I can't think of any reasonable constraints that would make it impractical.
-
- the function above is burried 15 calls deep, many of the functions in
- the call chain have no provisions for checking return values. As you
- say, calling exit() will at least terminate cleanly, but now I've got
- calls to exit() (or my own fatal_exit()) scattered all through my
- program which some might say is poor style.
-
- My only real contention is with "ALWAYS" - I don't like absolutes. :-)
-
- Dan
- --
- --------------------- message is author's opinion only --------------------
- J. Daniel Smith <DanS@bristol.com> http://www.bristol.com/~dan
- Bristol Technology Inc. +1 203 438 6969, 438-5013 (FAX)
- Ridgefield, Connecticut (USA) {info,jobs}@bristol.com
-